package com.teaching.teachingsupport.service.impl.studentserviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.teaching.teachingsupport.common.pojo.dataobject.Todo;
import com.teaching.teachingsupport.common.pojo.dataobject.User;
import com.teaching.teachingsupport.common.utils.SecurityUtils;
import com.teaching.teachingsupport.mapper.studentviewmapper.TodoMapper;
import com.teaching.teachingsupport.service.service2.studentservice.TodoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TodoServiceImpl implements TodoService {

    private final TodoMapper todoMapper;
    private final SecurityUtils securityUtils;

    @Override
    public List<Todo> getTodoByUsername(String username, String password) {
        // 验证用户名和密码
        User user = securityUtils.validateUser(username, password);
        if (user == null) {
            return List.of();
        }

        return getTodoByUserId(user.getUserId());
    }

    @Override
    public List<Todo> getTodoByUserId(Long userId) {
        return todoMapper.selectList(
            new LambdaQueryWrapper<Todo>()
                .eq(Todo::getUserId, userId)
                .orderByDesc(Todo::getCreatedTime)
        );
    }
}
